'use strict';

var marked = require('marked');
var Comment = require('../repos').Comment;

Comment.plugin('contentToHtml', {
  afterFind: function afterFind(comments) {
    comments.forEach(function (comment) {
      comment.content = marked(comment.content);
    });
    return comments;
  }
});

module.exports = {
  create: function create(comment) {
    return Comment.create(comment).exec();
  },

  delCommentById: function delCommentById(commentId, author) {
    return Comment.remove({ author: author, _id: commentId }).exec();
  },

  delCommentsByPostId: function delCommentsByPostId(postId) {
    return Comment.remove({ postId: postId }).exec();
  },

  getComments: function getComments(postId) {
    return Comment.find({ postId: postId }).populate({ path: 'author', model: 'User' }).sort({ _id: 1 }).addCreatedAt().contentToHtml().exec();
  },

  getCommentsCount: function getCommentsCount(postId) {
    return Comment.count({ postId: postId }).exec();
  }
};